package com.example.demo.controller;


import com.example.demo.common.Result;
import com.example.demo.mapper.UserInfoMapper;
import com.example.demo.mapper.VideoAllReplyMapper;
import com.example.demo.service.VideoAllReplyService;
import com.example.demo.utils.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 第五小组
 * @since 2021-08-04
 */
@RestController
@RequestMapping("/bilibili")

public class VideoAllReplyController {

    @Autowired
    VideoAllReplyService videoAllReplyService;

    @GetMapping("/reply")
    public Result reply(@RequestParam("bv") String bv, HttpServletResponse response) {
        Util.exec("\\脚本\\video_reply.py ", bv);
        return Result.succ(videoAllReplyService.listMaps());
    }

    @Resource
    VideoAllReplyMapper reply_count;/*某视频评论字数分布统计*/

    @GetMapping("/video/reply_count")
    public Result reply_count(@RequestParam("bv") String bv , HttpServletResponse response) {
        Util.exec("\\脚本\\video_reply.py ", bv);
        return Result.succ(reply_count.reply_count());
    }


    @Resource
    VideoAllReplyMapper level_count; /*某视频评论用户等级分布统计*/
    @GetMapping("/video/level_count")
    public Result level_count(@RequestParam("bv") String bv , HttpServletResponse response) {
        Util.exec("\\脚本\\video_reply.py ", bv);
        return Result.succ(level_count.level_count());
    }

    @Resource
    VideoAllReplyMapper sex_count;/*某视频评论用户性别分布统计*/
    @GetMapping("/video/sex_count")
    public Result sex_count(@RequestParam("bv") String bv , HttpServletResponse response) {
        Util.exec("\\脚本\\video_reply.py ", bv);
        return Result.succ(sex_count.sex_count());
    }


    @Resource
    VideoAllReplyMapper vip_count;/*某视频评论用户会员占比统计*/
    @GetMapping("/video/vip_count")
    public Result vip_count(@RequestParam("bv") String bv , HttpServletResponse response) {
        Util.exec("\\脚本\\video_reply.py ", bv);
        return Result.succ(vip_count.vip_count());
    }

    @Resource
    VideoAllReplyMapper time_count;/*某视频评论二十四小时分布情况统计*/
    @GetMapping("/video/time_count")
    public Result time_count(@RequestParam("bv") String bv , HttpServletResponse response) {
        Util.exec("\\脚本\\video_reply.py ", bv);
        return Result.succ(time_count.time_count());
    }

    @Resource
    VideoAllReplyMapper word_cloud;/*词云*/
    @GetMapping("/reply/word_cloud")
    public Result word_cloud(@RequestParam("bv") String bv , HttpServletResponse response) throws IOException {
        Util.exec("\\脚本\\video_reply.py", bv);
        String filePath=System.getProperty("user.dir")+"\\词频分析相关文件\\评论\\AllReply.txt";
        File file=new File(filePath);
        if(!file.exists())
        file.createNewFile();
        Writer writer=new FileWriter(filePath);
        BufferedWriter bw =new BufferedWriter(writer);
        for(String s:word_cloud.word_cloud()){
            bw.write(s);
            bw.newLine();
            bw.flush();
        }
        String picPath=System.getProperty("user.dir")+"\\词频分析相关文件\\背景\\1.jpg";
            String savePath=System.getProperty("user.dir")+"\\词频分析相关文件\\评论";
        Util.execs("\\脚本\\lexical_frequency_analysis.py",filePath,picPath,"white",savePath,"file","pic");
        return Result.succ(word_cloud.word_cloud());
    }

}



